package com.tansun.ruledata.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.tansun.rule.utils.IOUtils;
import com.tansun.rule.utils.JsonMessage;
import com.tansun.ruledata.common.ResponseVO;
import com.tansun.ruledata.data.service.BussinessSynchService;
import com.tansun.ruledata.data.service.RuleResourceLoadService;
import com.tansun.ruledata.entity.RuleDataInfo;
import com.tansun.ruledata.service.RuleDataBusinessService;
import com.tansun.ruledata.service.RuleDataCompileService;

@Controller
@RequestMapping(value = "/ruledeploy")
public class RuleDataDeployController {
	private Logger log = LoggerFactory.getLogger(this.getClass());
	@Autowired
    private  RuleResourceLoadService  ruleResourceLoadService;
	@Autowired
	private RuleDataBusinessService ruleDataBusinessService;

	@Autowired
	private BussinessSynchService bussinessSynchService;
	
	@Autowired
	RuleDataCompileService ruleDataCompileService;
	/**
	 * 部署
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/depolyDataRule.do")
	public @ResponseBody ResponseVO depolyRuleJar(HttpServletRequest request, HttpServletResponse response, ModelMap map){
		ResponseVO responseVO = new ResponseVO();
		String ruleObjectName = request.getParameter("ruleObjectName");
		String invokePoint = request.getParameter("invokePoint");
		try{
			ruleDataBusinessService.compileRuleData(ruleObjectName, invokePoint);
//			ruleResourceLoadService.sycCluster(ruleObjectName, invokePoint);  暂时注释，后续集群的时候考虑如何同步
			responseVO.setReturnData(ruleObjectName+"["+invokePoint+"]部署完成");
			responseVO.setStatus(ResponseVO.SUCCESS);
			log.info(ruleObjectName+"部署完成");
 	   }catch (Exception e) {
 		   	log.error(e.getMessage(), e);
 		   	Throwable root = ExceptionUtils.getRootCause(e);
			if( root==null ) {
				root = e;
			}
			String desDetail = ExceptionUtils.getFullStackTrace(e);;
			responseVO.setDesDetail(desDetail);
			responseVO.setReturnData(root.getMessage());
			responseVO.setStatus(ResponseVO.EXCEPTION);
	   }
	   return responseVO;
	}
//	@RequestMapping("/depolyDataRule.do")
//	public @ResponseBody String depolyRuleJar(HttpServletRequest request, HttpServletResponse response, ModelMap map)throws Exception{
//		String ruleObjectName = request.getParameter("ruleObjectName");
//		String invokePoint = request.getParameter("invokePoint");
//		try{
//			ruleDataBusinessService.compileRuleData(ruleObjectName, invokePoint);
////			ruleResourceLoadService.sycCluster(ruleObjectName, invokePoint);  暂时注释，后续集群的时候考虑如何同步
//			log.info(ruleObjectName+"部署完成");
// 	   }catch (Exception e) {
// 		  log.error(e.getMessage(), e);
// 		  throw new RuleDeployException("first deploy error!");
//	   }
//	   return "Yes";
//	}
	/**
	 * 同步集群部署状态
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/sycDataRule")
	public @ResponseBody String sycRuleJar(HttpServletRequest request, HttpServletResponse response, ModelMap map)throws Exception{
		String ruleObjectName = request.getParameter("ruleObjectName");
		String invokePoint = request.getParameter("invokePoint");
		try{
			ruleDataBusinessService.compileRuleData(ruleObjectName, invokePoint);
			System.out.println("同步完成");
 	   }catch (Exception e) {
 		  log.error("同步部署失败", e);
	   }
	   return "Yes";
	}
	
	/**
	 * 缓存规则数据并编译
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/cacheRuleData.do")
	public @ResponseBody JsonMessage cacheRuleData(HttpServletRequest request, HttpServletResponse response, ModelMap map)throws Exception{
		JsonMessage ret = new JsonMessage();
		try{
			String projectCode = request.getParameter("projectCode");
			String enterPoint = request.getParameter("enterPoint");
			ruleDataBusinessService.compileRuleData(projectCode, enterPoint);
 	   }catch (Exception e) {
			log.error(e.getMessage(), e);
			ret.setResult(false);
			ret.setMessage(IOUtils.getStackTraceMsg(e));
	   }
	   return ret;
	}

	/**
	 * 针对Rule_Exe前缀的数据库表
	 * @param projectCode
	 * @param enterPoint
	 * @param baseUrl
	 * @return
	 */
	@RequestMapping("/compileToCacheRuleExe.do")
	public @ResponseBody JsonMessage compileToCacheRuleExe(String projectCode,String enterPoint,String baseUrl) {
		JsonMessage ret = new JsonMessage();
		try {
			ruleDataCompileService.prepareDataToCacheRuleExe(null, projectCode, enterPoint);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			ret.setResult(false);
			ret.setMessage(e.getMessage());
		}
		return ret;
	}
	
	/******************************************************************************/
	
	
	/**
	 * 联机部署将规则平台数据存储到运行库
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/deployToBusinessRuleExeDB.do")
	public @ResponseBody JsonMessage deployToBusinessRuleExeDB(HttpServletRequest request, HttpServletResponse response, ModelMap map)throws Exception{
		JsonMessage ret = new JsonMessage();
		try{
			String ruleDataInfo = request.getParameter("ruleDataInfo");
			String projectCode = request.getParameter("projectCode");
			String timeStamp = String.valueOf(new Date().getTime());
			String enterPoints = bussinessSynchService.depolyRuleData(JSON.parseObject(ruleDataInfo,RuleDataInfo.class), projectCode,timeStamp);
			ruleDataCompileService.prepareDataToCacheRuleExe(null, projectCode, enterPoints);
 	   }catch (Exception e) {
			log.error(e.getMessage(), e);
			ret.setResult(false);
			ret.setMessage(IOUtils.getStackTraceMsg(e));
	   }
	   return ret;
	}
	

}
